<style lang="less">
    @import '../../../styles/common.less';
</style>

<template>
    <Card>
        <div style="margin-left: 30%;">
            <Row type="flex">
                 <Col span="2" order="2">
                    渠道 ：
                </Col>
                <!-- <Col order="2"  span="1.5">
                    <Checkbox
                    :indeterminate="channelIndeterminate"
                    :value="channelCheckAll"
                    @click.prevent.native="channelCheckAllOption">全选</Checkbox>
                </Col> -->
                <Col order="2"  span="10">
                <RadioGroup v-model="collectionCondition.channelGroup" @on-change="channelChange()"  >
                        <Radio  v-for="(item,index) in channelList"  :key="index" :label="item.channelCode+','+item.channelName" >{{item.channelName}}</Radio>
                </RadioGroup>
                </Col>
            </Row>

            <Row type="flex" style="margin-top:30px" v-if="collectionCondition.channelGroup!='' && collectionCondition.channelGroup!=null ">
                <Col span="2" order="2">
                    产品类型 ：
                </Col>
                <Col order="2" span="1.5">
                <Checkbox :value="productCheckAll"
                    :indeterminate="productIndeterminate"
                    @click.prevent.native="productCheckAllOption">全选</Checkbox>
                </col>
                <!-- <Col order="2" span="10">
                    <RadioGroup v-model="collectionCondition.productGroup" >
                        <Radio  v-for="(item,index) in productListStatic"  :key="index" :label="item.productType+','+item.productTypeName">{{item.productTypeName}}</Radio>
                    </RadioGroup>
                </Col> -->
                <Col order="2" span="10">
                    <CheckboxGroup v-model="collectionCondition.productGroup" >
                        <Checkbox  v-for="(item,index) in productListStatic"  :key="index" :label="item.productType+','+item.productTypeName">{{item.productTypeName}}</Checkbox>
                    </CheckboxGroup>
                </Col>
            </Row>

            <!-- <Row type="flex" style="margin-top:30px">
                <Col span="2" order="1">
                    资金源 ：
                </Col>
                <Col order="2" span="1.5">
                 <Checkbox :value="fundCheckAll"
                    :indeterminate="fundIndeterminate"
                    @click.prevent.native="fundCheckAllOption">全选</Checkbox>
                </col>
                <Col order="2" span="10">
                    <RadioGroup v-model="collectionCondition.fundCode">
                         <Radio  v-for="(item,index) in fundList"  :key="index" :label="item.fundCode">{{item.fundName}}</Radio>
                    </RadioGroup>
                </Col>
            </Row>-->

            <Row type="flex" style="margin-top:30px;font-size: 23px;">
                语音催收比例配置 ：
            </Row>

            <Row  style="margin-top:30px;" v-for="(item,index) in yyScaleList" :key="index">
                <Col order="1" span="3">{{yyScaleList[index].companyName}}:</Col>
                <Col order="2"><Input v-model="yyScaleList[index].radio"  style="width: 150px" /></Col>
            </Row>

            <Row type="flex" style="margin-top:30px;font-size: 23px;">
                人工催收比例配置  ：
            </Row>
            <Row  style="margin-top:30px;" v-for="(item,index) in rgScaleList" :key="index">
                <Col order="1" span="3">{{rgScaleList[index].companyName}}</Col>
                <Col order="2"><Input v-model="rgScaleList[index].radio"  style="width: 150px" /></Col>
            </Row>
            </Form>
            <div  style="margin-top:30px;margin-left: 50px;">
                <Button type="primary"  @click="commitSubmit">提交</Button>
                <Button type="primary"  @click="cancel" style="margin-left: 20px;">取消</Button>
            </div>
        </div>
    </Card>
</template>

<script>
export default {
  name: 'mutative-router',
  data () {
    return {
      isModify: '',
      channelIndeterminate: false,
      productIndeterminate: false,
      fundIndeterminate: false,
      channelCheckAll: false,
      productCheckAll: false,
      fundCheckAll: false,
      yyScaleList: [],
      rgScaleList: [],
      collectionCondition: {
        businessCode: '',
        scaleList: [],
        channelCode: '',
        productType: '',
        fundCode: '',
        channelGroup: '',
        productGroup: [],
        channelName: '',
        productTypeName: ''
      },
      channelList: [

      ],
      productListStatic: [

      ],
      productList: [

      ],

      fundList: [

      ]
    }
  },
  computed: {
    avatorImage () {
      return localStorage.avatorImgPath
    }
  },

  created () {
    // 初始化当前页面请求对象实例
    var vm = this
    this.$ajax = this.$util.ajax()
    this.baseInfoList()
    var params = this.$route.params.data
    if (params != null) {
      this.isModify = params.isModify
      this.collectionCondition.businessCode = params.businessCode
    }
    if (this.isModify === '1') { // 修改
      this.$ajax.post(this.$appContext.marvels_core_debtor + 'debtor/collection/intelligent/service/singleCollectionConfig', this.collectionCondition).then(re => {
        var data = re.data
        var eachData = data.data.map
        vm.collectionCondition.channelGroup = data.data.channelCode + ',' + data.data.channelName
        var requestData = { channelCode: data.data.channelCode }
        this.$ajax.post(this.$appContext.marvels_core_product + 'product/mcpt/service/selectByChannelCode', requestData).then(re => {
          var productData = re.data
          var code = productData.code
          var message = productData.message
          if (code !== 'success') {
            this.$Message.warning(message)
            return
          }
          vm.productListStatic = productData.data
          vm.productList = productData.data
        }).catch(er => {

        })
        Object.keys(eachData).forEach(function (key) {
          if (key === '1') {
            vm.yyScaleList = eachData[key]
          }
          if (key === '2') {
            vm.rgScaleList = eachData[key]
          }
        })
        vm.collectionCondition.productGroup.push(data.data.productType + ',' + data.data.productTypeName)
      }).catch(er => {

      })
    } else {
      this.companyList()
    }
  },

  methods: {
    commitSubmit () {
      if (this.isModify === '1') { // 修改
        this.$Modal.confirm({

          content: '确定修改?',

          okText: '确定',

          cancelText: '取消',
          onOk: () => {
            this.update()
          }
        })
      } else {
        this.$Modal.confirm({

          content: '确定新增?',

          okText: '确定',

          cancelText: '取消',
          onOk: () => {
            this.add()
          }
        })
      }
    },
    update () {
      var vm = this
      if (vm.collectionCondition.channelGroup == null || vm.collectionCondition.channelGroup === '') {
        this.$Message.warning('请选择渠道')
        return
      }
      if (vm.collectionCondition.productGroup == null || vm.collectionCondition.length < 1) {
        this.$Message.warning('请选择产品类型')
        return
      }
      var scaleList = []
      var yyScale = 0
      for (var i = 0; i < vm.yyScaleList.length; i++) {
        if (vm.yyScaleList[i].radio == null || vm.yyScaleList[i].radio === '') {
          this.$Message.warning(vm.yyScaleList[i].companyName + '语音催收比例不能为空')
          return
        } else {
          yyScale = yyScale + parseInt(vm.yyScaleList[i].radio)
        }
        scaleList.push(vm.yyScaleList[i])
      };
      if (yyScale !== 10) {
        this.$Message.warning('语音催收配置比例之和为10')
        return
      }
      var rgScale = 0
      for (var j = 0; j < vm.rgScaleList.length; j++) {
        if (vm.rgScaleList[j].radio == null || vm.rgScaleList[j].radio === '') {
          this.$Message.warning(vm.rgScaleList[j].companyName + '人工催收比例不能为空')
          return
        } else {
          rgScale = rgScale + parseInt(vm.rgScaleList[j].radio)
        }
        scaleList.push(vm.rgScaleList[j])
      };
      if (rgScale !== 10) {
        this.$Message.warning('人工催收配置比例之和为10')
        return
      }
      vm.collectionCondition.scaleList = scaleList
      vm.collectionCondition.channelCode = vm.collectionCondition.channelGroup.split(',')[0]
      vm.collectionCondition.channelName = vm.collectionCondition.channelGroup.split(',')[1]
      // vm.collectionCondition.productType=vm.collectionCondition.productGroup.split(',')[0];
      // vm.collectionCondition.productTypeName=vm.collectionCondition.productGroup.split(',')[1];
      this.$ajax.post(this.$appContext.marvels_core_debtor + 'debtor/collection/intelligent/service/updateCollectionConfig', vm.collectionCondition).then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code === 'success') {
          this.$router.push({ name: 'intelligent-collection-config-list' })
        } else {
          this.$Message.warning(message)
        }
      }).catch(er => {

      })
    },
    add () {
      var vm = this
      if (vm.collectionCondition.channelGroup == null || vm.collectionCondition.channelGroup === '') {
        this.$Message.warning('请选择渠道')
        return
      }
      if (vm.collectionCondition.productGroup == null || vm.collectionCondition.length < 1) {
        this.$Message.warning('请选择产品类型')
        return
      }
      var scaleList = []
      var yyScale = 0
      for (var i = 0; i < vm.yyScaleList.length; i++) {
        if (vm.yyScaleList[i].radio == null || vm.yyScaleList[i].radio === '') {
          this.$Message.warning(vm.yyScaleList[i].companyName + '语音催收比例不能为空')
          return
        } else {
          yyScale = yyScale + parseInt(vm.yyScaleList[i].radio)
        }
        scaleList.push(vm.yyScaleList[i])
      };
      if (yyScale !== 10) {
        this.$Message.warning('语音催收配置比例之和为10')
        return
      }
      var rgScale = 0
      for (var j = 0; j < vm.rgScaleList.length; j++) {
        if (vm.rgScaleList[j].radio == null || vm.rgScaleList[j].radio === '') {
          this.$Message.warning(vm.rgScaleList[j].companyName + '人工催收比例不能为空')
          return
        } else {
          rgScale = rgScale + parseInt(vm.rgScaleList[j].radio)
        }
        scaleList.push(vm.rgScaleList[j])
      };
      if (rgScale !== 10) {
        this.$Message.warning('人工催收配置比例之和为10')
        return
      }
      vm.collectionCondition.scaleList = scaleList
      vm.collectionCondition.channelCode = vm.collectionCondition.channelGroup.split(',')[0]
      vm.collectionCondition.channelName = vm.collectionCondition.channelGroup.split(',')[1]
      // vm.collectionCondition.productType=vm.collectionCondition.productGroup.split(',')[0];
      // vm.collectionCondition.productTypeName=vm.collectionCondition.productGroup.split(',')[1];
      this.$ajax.post(this.$appContext.marvels_core_debtor + 'debtor/collection/intelligent/service/addCollectionConfig', vm.collectionCondition).then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code === 'success') {
          this.$router.push({ name: 'intelligent-collection-config-list' })
        } else {
          this.$Message.warning(message)
        }
      }).catch(er => {

      })
    },
    cancel () {
      this.$Modal.confirm({

        content: '确定取消?',

        okText: '确定',

        cancelText: '取消',
        onOk: () => {
          this.$router.push({ name: 'intelligent-collection-config-list' })
        }
      })
    },
    channelCheckAllOption () {
      var vm = this
      if (this.channelIndeterminate) {
        this.channelCheckAll = false
      } else {
        this.channelCheckAll = !this.channelCheckAll
      }
      this.channelIndeterminate = false
      if (this.channelCheckAll) {
        for (var i = 0; i < vm.channelList.length; i++) {
          this.collectionCondition.channelGroup.push(vm.channelList[i].channelCode + ',' + vm.channelList[i].channelName)
        }
      } else {
        this.collectionCondition.channelGroup = []
      }
    },
    productCheckAllOption () {
      var vm = this
      if (this.productIndeterminate) {
        this.productCheckAll = false
      } else {
        this.productCheckAll = !this.productCheckAll
      }
      this.productIndeterminate = false
      if (this.productCheckAll) {
        for (var i = 0; i < vm.productList.length; i++) {
          this.collectionCondition.productGroup.push(vm.productList[i].productType + ',' + vm.productList[i].productTypeName)
        }
      } else {
        this.collectionCondition.productGroup = []
      }
    },
    fundCheckAllOption () {
      var vm = this
      if (this.fundIndeterminate) {
        this.fundCheckAll = false
      } else {
        this.fundCheckAll = !this.fundCheckAll
      }
      this.fundIndeterminate = false
      if (this.fundCheckAll) {
        for (var i = 0; i < vm.fundList.length; i++) {
          this.collectionCondition.fundGroup.push(vm.fundList[i].fundCode + ',' + vm.fundList[i].fundName)
        }
      } else {
        this.collectionCondition.fundGroup = []
      }
    },
    conditionInit () {
      this.baseInfoList()
      this.companyList()
    },
    baseInfoList () {
      var vm = this
      this.$ajax.post(this.$appContext.marvels_core_approve + 'approve/loanList/service/baseCondition').then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code !== 'success') {
          this.$Message.warning(message)
          return
        }
        vm.channelList = data.data.channelList
        vm.fundList = data.data.fundList
      }).catch(er => {

      })
    },
    companyList () {
      var vm = this
      this.$ajax.post(this.$appContext.marvels_core_debtor + 'debtor/collection/intelligent/service/collectionCompanyInfo').then(re => {
        var data = re.data
        var code = data.code
        var message = data.message

        if (code !== 'success') {
          this.$Message.warning(message)
          return
        }
        Object.keys(data.data).forEach(function (key) {
          if (key === '1') {
            vm.yyScaleList = data.data[key]
          }
          if (key === '2') {
            vm.rgScaleList = data.data[key]
          }
        })
      }).catch(er => {

      })
    },
    channelChange () {
      var vm = this
      if (vm.collectionCondition.channelGroup == null || vm.collectionCondition.channelGroup === '') {
        return
      }
      // for(var i=0;i<vm.collectionCondition.channelGroup.length;i++){
      //     for(var j=0;j<vm.productList.length;j++){
      //         if(vm.collectionCondition.channelGroup[i].split(',')[2]==vm.productListStatic[j].patternType){
      //             tmp.push(vm.productList[j]);
      //         }
      //     }
      // }
      // for(var j=0;j<vm.productList.length;j++){
      //     if(vm.collectionCondition.channelGroup.split(',')[2]==vm.productList[j].patternType){
      //         tmp.push(vm.productList[j]);
      //     }
      // }
      var data = { channelCode: vm.collectionCondition.channelGroup.split(',')[0] }
      this.$ajax.post(this.$appContext.marvels_core_product + 'product/mcpt/service/selectByChannelCode', data).then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code !== 'success') {
          this.$Message.warning(message)
          return
        }
        vm.productList = data.data
        vm.productListStatic = data.data
      }).catch(er => {

      })
    }

  }

}
</script>
<style>

</style>
